# LIV DATA Clean Individual
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)

indata      = paste0(WD,"/_liv/","", collapse = NULL)
 
library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)

lagpad   = function(x, k) {
  if (!is.vector(x)) 
    stop('x must be a vector')
  if (!is.numeric(x)) 
    stop('x must be numeric')
  if (!is.numeric(k))
    stop('k must be numeric')
  if (1 != length(k))
    stop('k must be a single number')
  c(rep(NA, k), x)[1 : length(x)] 
}


##### LIV GDP #########
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "RGDPX")

data$RGDPX_ZM      = as.numeric(data$RGDPX_ZM) 
data$RGDPX_BP      = as.numeric(data$RGDPX_BP) 
data$RGDPX_6M      = as.numeric(data$RGDPX_6M) 
data$RGDPX_12M     = as.numeric(data$RGDPX_12M) 

data$f1       = 100*((data$RGDPX_6M/data$RGDPX_BP)^(4/3)-1)
data$f1r      = 200*(data$RGDPX_6M/data$RGDPX_ZM-1)
data$f2r      = 200*(data$RGDPX_12M/data$RGDPX_6M-1)

data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2

data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data$id         = data$ID

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r) )

data_clean       = merge(data_f1, data_f2, all = "TRUE")

tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter, y, fly))

data_clean      = merge(data_clean, data_tmp, all = "TRUE")

data            = data_clean
data$fc_err     = data$y-data$f1
data$fc_rev     = data$f1r-data$f2r 
data$y_realz    = data$y 
data$f1         = data$f1
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]

data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))

save(data,file="us_liv_rgdp_ind.rda")
clear()


##### LIV CPI #########
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)

tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "CPI")

data$CPI_BP      = as.numeric(data$CPI_BP) 
data$CPI_ZM      = as.numeric(data$CPI_ZM) 
data$CPI_6M      = as.numeric(data$CPI_6M) 
data$CPI_12M     = as.numeric(data$CPI_12M) 

data$f1       = 100*((data$CPI_6M/data$CPI_BP)^(4/3)-1)
data$f1r      = 200*(data$CPI_6M/data$CPI_ZM-1)
data$f2r      = 200*(data$CPI_12M/data$CPI_6M-1)

data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2

data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data$id         = data$ID

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r))

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r))

data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_clean$fc_rev= data_clean$f2r-data_clean$f1r

tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter,pi,flpi))

data_clean      = merge(data_clean, data_tmp, all = "TRUE")

data            = data_clean
data$fc_err     = data$pi-data$f1
data$p_realz    = data$pi
data$f1         = data$f1r
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]

data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev,flpi))

save(data,file="us_liv_cpi_ind.rda")
clear()